package com.zxl.grocery.microservice.crawler.service.impl;

import com.zxl.grocery.microservice.crawler.common.base.AntAction;
import com.zxl.grocery.microservice.crawler.common.base.AntInfo;
import com.zxl.grocery.microservice.crawler.service.WebAntManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Service
public class WebAntManagerImpl implements WebAntManagerService {
    
    @Autowired
    Map<String, AntAction> antActionMap;
    @Override
    public AntInfo queryObject(String id) {
        AntAction antAction = antActionMap.get(id);
        AntInfo antInfo=new AntInfo();
        antInfo.setId(id);
        antInfo.setStatus(antAction.status());
        antInfo.setName(antAction.getClass().getName());
        return antInfo;
    }

    @Override
    public List<AntInfo> queryList(Map<String, Object> map) {
        List<AntInfo> AntInfos=new ArrayList<AntInfo>();
        for(Entry<String, AntAction> entry: antActionMap.entrySet()) { 
            AntAction antAction = entry.getValue();
            String id = entry.getKey();
            AntInfo antInfo=new AntInfo();
            antInfo.setId(id);
            antInfo.setStatus(antAction.status());
            antInfo.setName(antAction.getClass().getName());
            AntInfos.add(antInfo);
        }
        return AntInfos;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        
        return antActionMap.size();
    }

    @Override
    public void start(String id) {
        AntAction antAction = antActionMap.get(id);
        antAction.start();
        
    }

    @Override
    public void restart(String id) {
        AntAction antAction = antActionMap.get(id);        
        antAction.restart();
    }

    @Override
    public void stop(String id) {
        AntAction antAction = antActionMap.get(id);         
        antAction.stop();
    }

    @Override
    public void start(String[] ids) {
      for (String id : ids) {
        this.start(id);
      }
    }

    @Override
    public void restart(String[] ids) {
        for (String id : ids) {
            this.restart(id);
          }
    }

    @Override
    public void stop(String[] ids) {
        for (String id : ids) {
            this.stop(id);
       }
    }
    
}
